package sk.activities;

import java.util.ArrayList;
import java.util.List;
import sk.database.Server;
import sk.model.Player;
import sk.model.Session;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class StartActivity extends Activity {
	private static final String FIRST_START_BOOL = "ersterStartBoolean";
	public static final String START_ACTIVITY_STORAGE = "startActivitySpeicher";
	public static final String DEVICE_NAME = "absendeName";
	private static final String SESSION_RUNNING = "sessionAmLaufen";
	private SharedPreferences storage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_start);

		storage =
			getApplicationContext().getSharedPreferences(
				START_ACTIVITY_STORAGE, MODE_PRIVATE);
		boolean sessionRunning = storage.getBoolean(SESSION_RUNNING, false);
		if (sessionRunning) {
			findViewById(R.id.startSession).setVisibility(View.GONE);
		} else {
			findViewById(R.id.endSession).setVisibility(View.GONE);
			findViewById(R.id.progressSession).setVisibility(View.GONE);
		}

		boolean firstStart = storage.getBoolean(FIRST_START_BOOL, true);
		if (firstStart) {
			showDialog(2);
		}

	}

	public void startSession(View view) {
		showDialog(0);
		final SharedPreferences.Editor editor = storage.edit();
		editor.putBoolean(SESSION_RUNNING, true);
		editor.commit();
		findViewById(R.id.startSession).setVisibility(View.GONE);
		findViewById(R.id.endSession).setVisibility(View.VISIBLE);
		findViewById(R.id.progressSession).setVisibility(View.VISIBLE);
	}

	public void endSession(View view) {
		final SharedPreferences.Editor editor = storage.edit();
		editor.putBoolean(SESSION_RUNNING, false);
		editor.commit();
		findViewById(R.id.startSession).setVisibility(View.VISIBLE);
		findViewById(R.id.endSession).setVisibility(View.GONE);
		findViewById(R.id.progressSession).setVisibility(View.GONE);
	}

	public void progressSession(View view) {

		// open Session activity
		Intent intent = new Intent(StartActivity.this, SessionActivity.class);
		intent.putExtra(SessionActivity.EXTRA_NEW_SESSION, false);
		startActivity(intent);
	}

	@Override
	public Dialog onCreateDialog(int id) {

		if (id == 0) {
			// Create Dialog to choose number of players
			final CharSequence[] valuePlayerCount =
				{ "4 Spieler", "5 Spieler", "6 Spieler" };
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Spieleranzahl?");

			builder.setCancelable(false);

			builder.setItems(valuePlayerCount,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {

						// open Session activity
						Intent intent =
							new Intent(StartActivity.this,
								SessionActivity.class);
						intent
							.putExtra(SessionActivity.EXTRA_NEW_SESSION, true);
						intent.putExtra(getString(R.string.extra_player_count),
							4 + which);
						startActivity(intent);
					}
				});
			return builder.create();
		} else if (id == 1) {

			// Create Dialog to warn no players were loaded
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Achtung");
			builder.setMessage(getString(R.string.no_players_loaded));
			builder.setPositiveButton("OK", null);
			return builder.create();
		} else if (id == 2) {

			// Create Dialog to get device name
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Achtung");
			builder.setMessage(getString(R.string.device_name));
			final EditText input = new EditText(this);
			builder.setView(input);

			// Set up the buttons
			builder.setPositiveButton("OK",
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						saveDeviceName(input.getText().toString());
					}
				});
			return builder.create();
		} else if (id == 3) {
			// Create Dialog to say players were loaded
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Super");
			builder.setMessage(getString(R.string.players_loaded));
			builder.setPositiveButton("OK", null);
			return builder.create();
		} else {
			return null;
		}
	}

	protected void saveDeviceName(String deviceName) {
		final SharedPreferences.Editor editor = storage.edit();
		editor.putString(DEVICE_NAME, deviceName);
		editor.putBoolean(FIRST_START_BOOL, false);
		editor.commit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.start, menu);
		return true;
	}

	public void getPlayers(View view) {
		// final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading");
		// new Thread() {
		//
		// public void run() {
		//
		getPlayerList();
		//
		// // dismiss the progress dialog
		//
		// dialog.dismiss();
		//
		// }
		//
		// }.start();

	}

	private void getPlayerList() {
		Player[] players = Server.getPlayerList();
		if (players != null) {
			SharedPreferences storage =
				getApplicationContext().getSharedPreferences(
					SessionActivity.STORAGE_NAME, MODE_PRIVATE);
			SharedPreferences.Editor editor = storage.edit();
			editor.putInt(SessionActivity.SAVE_NUMBER_OF_PLAYERS,
				players.length);

			// get list of available Players from saved data
			for (int i = 0; i < players.length; i++) {
				editor.putString(SessionActivity.SAVE_PRE_NAME + i,
					players[i].getPreName());
				editor.putString(SessionActivity.SAVE_SUR_NAME + i,
					players[i].getSurName());
				editor.putInt(SessionActivity.SAVE_PLAYER_ID + i,
					players[i].getId());
			}
			editor.commit();
			showDialog(3);
		} else {
			showDialog(1);
		}
	}

	public void sendSessions(View view) {
		List<Session> sessions = new ArrayList<Session>();
		SharedPreferences storage =
			getApplicationContext().getSharedPreferences(
				SessionActivity.STORAGE_NAME, MODE_PRIVATE);
		int numberOfSessions =
			storage.getInt(SessionActivity.SAVE_SESSION_COUNT, 0);
		for (int i = 0; i < numberOfSessions; i++) {
			String ses =
				storage.getString(SessionActivity.SAVE_SESSION + i, "");
			if (!ses.equals("")) {
				sessions.add(new Session(ses));
			}
		}
	}

	public void showSessions(View view) {

		// open ShowSessions activity
		Intent intent =
			new Intent(StartActivity.this, ShowSessionsActivity.class);
		startActivity(intent);
	}
}
